/*
 * @Date: 2021-09-12 19:21:05
 * @Author: Acckno1
 * @LastEditTime: 2021-09-12 19:22:11
 * @Description: 
 */
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int m;
string name, start, endt;

int calc(string time) {
    int sc = stoi(time.substr(6, 2));
    int mi = stoi(time.substr(3, 2));
    int hr = stoi(time.substr(0, 2));
    return hr * 60 * 60 + mi * 60 + sc;
}

int main() {
    cin >> m;
    string signin = "";
    string signout = "";
    int si = 0x3f3f3f3f;
    int so = 0;
    for (int i = 0; i < m; i ++ ) {
        cin >> name >> start >> endt;
        int s = calc(start);
        int e = calc(endt);
        if (e > so) so = e, signout = name;
        if (s < si) si = s, signin = name;
    }
    cout << signin << " " << signout << endl;
     
    return 0;
}